/**
 * Copyright ©2012-2018 HopeHaiyang Corporation, All Rights Reserved
 * http://www.wuhanhope.com
 */
package hope.platform.data.server.data.img;

import hope.platform.data.dao.ScheduleRepository;
import hope.platform.data.domain.Schedule;
import hope.platform.data.server.data.header.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;

/**
 * class info
 *
 * @author hujia
 *         Create at 2018/7/25 16:00
 */
@Service
public class ScheduleServiceImg implements ScheduleService {

    @Autowired
    private ScheduleRepository repository;

    @Override
    public Schedule addSchedule(Schedule schedule) {
        schedule.setGmtModified(new Date());
        schedule.setGmtCreate(new Date());
        return repository.save(schedule);
    }

    @Override
    public Schedule updateSchedule(Schedule schedule) {
        Optional<Schedule> scheduleOptional = repository.findById(schedule.getId());
        if (scheduleOptional.isPresent()) {
            if (null != schedule.getBeginTime()) {
                scheduleOptional.get().setBeginTime(schedule.getBeginTime());
            }
            if (null != schedule.getEndTime()) {
                scheduleOptional.get().setEndTime(schedule.getEndTime());
            }
            scheduleOptional.get().setGmtModified(new Date());
        }
        return repository.save(scheduleOptional.get());
    }

    @Override
    public Page<Schedule> findAll(Pageable pageable) {
        return repository.findAll(pageable);
    }
}
